package com.estronger.shopmanage.ui.othertask.presenter;

import android.content.Context;

import com.estronger.shopmanage.ui.othertask.activity.OtherTaskMainActivity;
import com.estronger.shopmanage.ui.othertask.bean.OtherTaskMainBean;
import com.estronger.shopmanage.ui.othertask.fragment.BaseOtherTaskMainFragment;
import com.google.gson.Gson;
import com.soonfor.evaluate.base.IEvalBasePresenter;
import com.soonfor.evaluate.http.api.Request;
import com.soonfor.evaluate.http.httptools.AsyncUtils;

import org.json.JSONObject;

/**
 * Created by DingYg on 2018-01-29.
 */

public class OtherTaskMainPresenter implements IEvalBasePresenter, AsyncUtils.AsyncCallback {

    private OtherTaskMainActivity mActivity;
    private BaseOtherTaskMainFragment bgFragment;
    private int type = 1;
    private int viewType = -1;

    public OtherTaskMainPresenter(OtherTaskMainActivity mActivity) {
        this.mActivity = mActivity;
    }

    public OtherTaskMainPresenter(BaseOtherTaskMainFragment bgFragment) {
        this.bgFragment = bgFragment;
    }
    /**
     *
     * @param mContext
     * @param viewType 请求这个接口的界面类型 0 Activity 1 Fragment
     */
    public void getTaskNum(Context mContext, int viewType){
        this.viewType = viewType;
        Request.OtherTask.getOtherTaskNum(mContext, this);
    }

    /**
     * 获取任务列表
     * @param type
     * @param pageNo
     * @param isNeedProgressDialog
     */
    public void getData(int type, int pageNo, boolean isNeedProgressDialog) {
        this.type = type;
//        if (isNeedProgressDialog) {
//            this.bgFragment.showLoadingDialog();
//        }
        Request.OtherTask.getOtherTaskList(bgFragment.getContext(), type, pageNo,this);
    }

    @Override
    public void success(int requestCode, String data) {
        switch (requestCode) {
            case Request.OtherTask.GET_OTHERTASKNUM://任务数量
                int nums[] = {0,0,0};
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    nums[0] = jsonObject.getInt("fworknum1");
                    nums[1] = jsonObject.getInt("fworknum2");
                    nums[2] = jsonObject.getInt("fworknum3");
                }catch (Exception e){}
                if(viewType==0){
                    mActivity.setNumToTitles(nums);
                }else if(viewType == 1){
                    bgFragment.setNumToTitles(nums);
                }
                break;
            case Request.OtherTask.GET_OTHERTASKLIST:
                bgFragment.closeLoadingDialog();
                try {
                    OtherTaskMainBean taskMainBean = new Gson().fromJson(data, OtherTaskMainBean.class);
                    bgFragment.setListView(type, taskMainBean.getData());
                } catch (Exception e) {
                    bgFragment.showError(e.toString());
                }
                break;
//            case Request.ACCEPT_TASK:
//                HeadBean headBean = RepJsonUtils.getHeadBean(object.toString());
//                if (headBean != null) {
//                    if(headBean.getMsgcode()==0 || headBean.isSuccess())
//                        bgFragment.refreshAfterAccept(ItemPosition);
//                    else
//                        MyToast.showFailToast(bgFragment.getContext(), headBean.getFaileMsg());
//                }
//                break;
//            case Request.REJECT_TASK:
//                HeadBean headBean1 = RepJsonUtils.getHeadBean(object.toString());
//                if (headBean1 != null) {
//                    if((headBean1.getMsgcode()==0 || headBean1.isSuccess()))
//                        bgFragment.refreshAfterReject(ItemPosition);
//                    else
//                        MyToast.showFailToast(bgFragment.getContext(), headBean1.getFaileMsg());
//                }
//                break;
        }
    }

    @Override
    public void fail(int requestCode, int statusCode, String data, String msg) {
        switch (requestCode) {
            case Request.OtherTask.GET_OTHERTASKLIST:
                bgFragment.closeLoadingDialog();
                bgFragment.showNoDataHint(msg);
                break;
//            case Request.GET_OPTION:
//                mActivity.closeLoadingDialog();
//                //mActivity.showNoDataHint();
//                break;
//            // bgFragment.showFailText(bgFragment.getContext(), throwable, errorResponse);
        }
    }
}
